package com.encryption.feature.doc.ui.render
{
	import com.encryption.feature.doc.DocTypeManager;
	
	import mx.containers.HBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.core.ScrollPolicy;
	import mx.events.ResizeEvent;
	
	public class DocNameItemRenderer extends HBox
	{
		private var _fileImage:Image;
		private var _fileLabel:Label;
		
		private var _fileSource:Class;
		private var _fileName:String;
		
		private var _data:Object;
		
		public function DocNameItemRenderer()
		{
			super();
			setStyle("verticalAlign", "middle");
			setStyle("horizontalGap", 8);
			horizontalScrollPolicy = ScrollPolicy.OFF;
			
			addEventListener(ResizeEvent.RESIZE, resizeHandle);
		}
		
		private function resizeHandle(event:ResizeEvent):void
		{
			if(width == 0 || isNaN(width))
				return;
			
			_fileLabel.maxWidth = width > 50 ? width - 50 : 50;
		}
		
		override public function get data():Object
		{
			return _data;
		}
		
		override public function set data(value:Object):void
		{
			super.data = value;
			
			if(_data == value)
				return;
			
			_data = value;
			
			_fileSource = _data != null ? DocTypeManager.instance.getTypeImage(_data.type) : null;
			_fileName = _data != null ? _data.name : "";
			if(_fileImage != null)
				_fileImage.source = _fileSource;
			if(_fileLabel != null)
				_fileLabel.text = _fileName;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if(_fileImage == null){
				_fileImage = new Image();
				_fileImage.source = _fileSource;
				addChild(_fileImage);
			}
			
			if(_fileLabel == null){
				_fileLabel = new Label();
				_fileLabel.text = _fileName;
				_fileLabel.maxWidth = 200;
				addChild(_fileLabel);
			}
		}

	}
}